package com.getpebble.android.main.sections.mypebble.view;

import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentValues;
import android.content.Context;
import android.content.Loader;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.database.DatabaseUtils;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView.ScaleType;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.widget.AsyncImageView;
import com.getpebble.android.common.model.LoadedApp;
import com.getpebble.android.common.model.PblInstalledAppDataModel;
import com.getpebble.android.common.provider.model.PblInstalledJoinLockerAppDataModel;
import com.sun.mail.smtp.SMTPMessage;
import java.util.HashMap;

public class LoadedAppsCarousel extends Gallery implements LoaderCallbacks<Cursor> {
    public static final String TAG = LoadedAppsCarousel.class.getSimpleName();
    private LoadedAppsAdapter mAdapter;
    private DataSetObserver mDataSetObserver = null;
    private int mItemHeight;
    private int mItemWidth;
    private LoaderManager mLoaderManager;

    public class LoadedAppsAdapter extends CursorAdapter {
        private HashMap<Integer, LoadedApp> mLoadedAppHashMap = new HashMap();

        public LoadedAppsAdapter(Context context, Cursor cursor) {
            super(context, cursor, 2);
        }

        public LoadedApp getLoadedApp(int position) {
            if (this.mLoadedAppHashMap.containsKey(Integer.valueOf(position))) {
                return (LoadedApp) this.mLoadedAppHashMap.get(Integer.valueOf(position));
            }
            if (getCursor() == null || getCursor().getCount() == 0 || position < 0) {
                return null;
            }
            getCursor().moveToPosition(position);
            ContentValues contentValues = new ContentValues();
            DatabaseUtils.cursorRowToContentValues(getCursor(), contentValues);
            LoadedApp loadedApp = LoadedApp.fromContentValues(contentValues);
            this.mLoadedAppHashMap.put(Integer.valueOf(position), loadedApp);
            return loadedApp;
        }

        public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
            AsyncImageView view = new AsyncImageView(LoadedAppsCarousel.this.getContext());
            view.setLayoutParams(new LayoutParams(LoadedAppsCarousel.this.mItemWidth, LoadedAppsCarousel.this.mItemHeight));
            view.setScaleType(ScaleType.FIT_XY);
            return view;
        }

        public void bindView(View view, Context context, Cursor cursor) {
            LoadedApp loadedApp = getLoadedApp(cursor.getPosition());
            if (loadedApp == null) {
                Trace.error(LoadedAppsCarousel.TAG, "Unable to get loaded app for position: " + cursor.getPosition());
                return;
            }
            AsyncImageView imageView = (AsyncImageView) view;
            String screenshotImage = loadedApp.getScreenshotImage();
            if (screenshotImage != null) {
                imageView.fetch(screenshotImage);
            } else {
                imageView.setImageResource(R.drawable.watch_faces_icon);
            }
        }

        protected void onContentChanged() {
            super.onContentChanged();
            this.mLoadedAppHashMap.clear();
        }
    }

    public LoadedAppsCarousel(Context context) {
        super(context);
    }

    public LoadedAppsCarousel(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoadedAppsCarousel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setDimensions(int itemWidth, int itemHeight) {
        if (itemHeight != this.mItemHeight || itemWidth != this.mItemWidth) {
            this.mItemWidth = itemWidth;
            this.mItemHeight = itemHeight;
            if (PebbleApplication.getConnectedDevice() != null) {
                initCursor();
            }
        }
    }

    public void setLoaderManager(LoaderManager loaderManager) {
        this.mLoaderManager = loaderManager;
    }

    public void onConnectedDeviceUpdated() {
        if (PebbleApplication.getConnectedDevice() != null) {
            restartLoader();
            return;
        }
        if (this.mAdapter != null) {
            this.mAdapter.swapCursor(null);
        }
        if (this.mDataSetObserver != null) {
            this.mDataSetObserver.onChanged();
            this.mDataSetObserver.onInvalidated();
        }
    }

    public void setDataSetObserver(DataSetObserver dataSetObserver) {
        if (!(this.mDataSetObserver == null || this.mAdapter == null)) {
            this.mAdapter.unregisterDataSetObserver(this.mDataSetObserver);
        }
        this.mDataSetObserver = dataSetObserver;
        if (this.mAdapter != null) {
            this.mAdapter.registerDataSetObserver(this.mDataSetObserver);
        }
    }

    private void initCursor() {
        if (this.mLoaderManager == null) {
            Trace.warning(TAG, "initCursor(): mLoaderManager is null");
        } else {
            this.mLoaderManager.initLoader(2, null, this);
        }
    }

    public void restartLoader() {
        if (this.mLoaderManager == null) {
            Trace.warning(TAG, "initCursor(): mLoaderManager is null");
        } else {
            this.mLoaderManager.restartLoader(2, null, this);
        }
    }

    public Loader<Cursor> onCreateLoader(int loaderId, Bundle bundle) {
        switch (loaderId) {
            case SMTPMessage.RETURN_HDRS /*2*/:
                return PblInstalledJoinLockerAppDataModel.fetchApps(getContext(), PebbleApplication.getConnectedDevice());
            default:
                Trace.warning(TAG, "Unknown loader id: " + loaderId);
                return null;
        }
    }

    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        if (cursorLoader == null) {
            Trace.error(TAG, "onLoadFinished: CursorLoader was null");
        } else if (cursor == null) {
            Trace.error(TAG, "onLoadFinished: Cursor was null");
        } else {
            switch (cursorLoader.getId()) {
                case SMTPMessage.RETURN_HDRS /*2*/:
                    cursor.setNotificationUri(getContext().getContentResolver(), PblInstalledAppDataModel.TABLE_URI);
                    if (this.mAdapter != null) {
                        this.mAdapter.swapCursor(cursor);
                        return;
                    } else if (this.mItemHeight > 0) {
                        this.mAdapter = new LoadedAppsAdapter(getContext(), cursor);
                        setAdapter(this.mAdapter);
                        if (this.mDataSetObserver != null) {
                            this.mAdapter.registerDataSetObserver(this.mDataSetObserver);
                            this.mDataSetObserver.onChanged();
                            return;
                        }
                        return;
                    } else {
                        return;
                    }
                default:
                    Trace.warning(TAG, "Got loadFinished from unknown loaderId: " + cursorLoader.getId());
                    return;
            }
        }
    }

    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        switch (cursorLoader.getId()) {
            case SMTPMessage.RETURN_HDRS /*2*/:
                if (this.mAdapter != null) {
                    this.mAdapter.swapCursor(null);
                    return;
                }
                return;
            default:
                return;
        }
    }
}
